﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ServiceApplication.Service.WorkFlow.WorkflowDesignerApp
{
    public partial class FrmProcDesgin : Form
    {
        private static FrmProcDesgin instance;

        public FrmProcDesgin()
        {
            InitializeComponent();
            this.usrCtrlProcs1.Initialize();
        }

        public static FrmProcDesgin GetInstance()
        {
            if (instance == null || instance.IsDisposed)
                instance = new FrmProcDesgin();
            return instance;
        }

        private void tStripCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tStripDone_Click(object sender, EventArgs e)
        {
            //GetImage();
            if (!this.usrCtrlProcs1.ValidateInputs)
            {
                MessageBox.Show("请填写所有流程");
                return;
            }
            var name = this.tStripTxtProcName.Text.Trim();
            if (name == String.Empty)
                name = "Default.jpg";
            else
                name += ".jpg";
            var img1 = GetImage();
            img1.Save(name);
        }

        public Image GetImage()
        {
            Image img = new Bitmap(this.Width, this.Height);
            Graphics g = Graphics.FromImage(img);
            g.CopyFromScreen(new Point(this.Location.X + 5, this.Location.Y + 55), new Point(0, 0), new Size(this.Width - 10, this.Height - 80));
            g.Dispose();
            return img;
        }

        #region 截图用

        public static Bitmap GetWindow(IntPtr hWnd)
        {
            IntPtr hscrdc = GetWindowDC(hWnd);
            Control control = Control.FromHandle(hWnd);
            IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, control.Width, control.Height);
            IntPtr hmemdc = CreateCompatibleDC(hscrdc);
            SelectObject(hmemdc, hbitmap);
            PrintWindow(hWnd, hmemdc, 0);
            Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
            DeleteDC(hscrdc);//删除用过的对象
            DeleteDC(hmemdc);//删除用过的对象
            return bmp;
        }



        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateDC(
        string lpszDriver, // driver name驱动名
        string lpszDevice, // device name设备名
        string lpszOutput, // not used; should be NULL
        IntPtr lpInitData // optional printer data
        );
        [DllImport("gdi32.dll")]
        public static extern int BitBlt(
        IntPtr hdcDest, // handle to destination DC目标设备的句柄

        int nXDest, // x-coord of destination upper-left corner目标对象的左上角的X坐标
        int nYDest, // y-coord of destination upper-left corner目标对象的左上角的Y坐标
        int nWidth, // width of destination rectangle目标对象的矩形宽度
        int nHeight, // height of destination rectangle目标对象的矩形长度
        IntPtr hdcSrc, // handle to source DC源设备的句柄
        int nXSrc, // x-coordinate of source upper-left corner源对象的左上角的X坐标
        int nYSrc, // y-coordinate of source upper-left corner源对象的左上角的Y坐标
        UInt32 dwRop // raster operation code光栅的操作值
        );
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(
        IntPtr hdc // handle to DC
        );
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(
        IntPtr hdc, // handle to DC
        int nWidth, // width of bitmap, in pixels
        int nHeight // height of bitmap, in pixels
        );
        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(
        IntPtr hdc, // handle to DC
        IntPtr hgdiobj // handle to object
        );
        [DllImport("gdi32.dll")]
        public static extern int DeleteDC(
        IntPtr hdc // handle to DC
        );
        [DllImport("user32.dll")]
        public static extern bool PrintWindow(
        IntPtr hwnd, // Window to copy,Handle to the window that will be copied. 
       IntPtr hdcBlt, // HDC to print into,Handle to the device context. 
       UInt32 nFlags // Optional flags,Specifies the drawing options. It can be one of the following values. 
       );
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(
        IntPtr hwnd);

        #endregion

        private void tStripBtnRefresh_Click(object sender, EventArgs e)
        {
            var countStr = this.tStripTxtCount.Text.Trim();
            var count = 0;
            try
            {
                count = Convert.ToInt32(countStr);
                if (count <= 0 || count > 10)
                    MessageBox.Show("请输入正确数组1~10之间");
                else
                    this.usrCtrlProcs1.ProcCount = count;
            }
            catch
            {
                MessageBox.Show("请输入正确数组1~10之间");
            }
            finally
            {
                this.tStripTxtCount.Text = String.Empty;
            }
        }
    }
}
